package kubernetes

import (
	"k8s-admin/service/kubernetes"
	"k8s-admin/utils"

	"github.com/gin-gonic/gin"
)

/*列表*/
func GetClusterConfigMapList(ctx *gin.Context) {
	params := new(struct {
		Namespace string `form:"namespace" binding:"required"`
	})
	if err := ctx.ShouldBind(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
		return
	}
	cid := ctx.Param("cid")
	ComfigMapList, err := kubernetes.NewK8sInterface().GetClusterConfigMapList(cid, params.Namespace)
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
		return
	}
	NewConfigMapList := []map[string]interface{}{}
	for _, item := range ComfigMapList.Items {
		NewConfigMapList = append(NewConfigMapList, map[string]interface{}{
			"name":               item.Name,
			"namespace":          item.Namespace,
			"uid":                item.UID,
			"creation_timestamp": item.CreationTimestamp,
			"annotations":        item.Annotations,
		})

	}
	utils.ReturnContext(ctx).Successful("成功", NewConfigMapList)
}

/*详情*/
func GetClusterConfigMapInfo(ctx *gin.Context) {
	params := new(struct {
		Namespace     string `form:"namespace" binding:"required"`
		ConfigMapName string `form:"configmap_name" binding:"required"`
	})
	if err := ctx.ShouldBind(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
		return
	}
	cid := ctx.Param("cid")
	ComfigMap, err := kubernetes.NewK8sInterface().GetClusterConfigMapInfo(cid, params.Namespace, params.ConfigMapName)
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		utils.ReturnContext(ctx).Successful("成功", ComfigMap)
	}

}

/*删除*/
func DeleteClusterConfigMap(ctx *gin.Context) {
	params := new(struct {
		Namespace     string `form:"namespace" json:"namespace" binding:"required"`
		ConfigMapName string `form:"configmap_name" json:"configmap_name" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
		return
	}
	err := kubernetes.NewK8sInterface().DeleteClusterConfigMap(cid, params.Namespace, params.ConfigMapName)
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		utils.ReturnContext(ctx).Successful("成功", "删除成功")
	}
}

/*更新*/
func UpdateClusterConfigMap(ctx *gin.Context) {
	params := new(struct {
		Namespace     string `json:"namespace" binding:"required"`
		ConfigMapData string `json:"configmap_data" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
		return
	}
	err := kubernetes.NewK8sInterface().UpdateClusterConfigMap(cid, params.Namespace, params.ConfigMapData)
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		utils.ReturnContext(ctx).Successful("成功", "更新成功")
	}
}

/*创建*/
func CreateClusterConfigMap(ctx *gin.Context) {
	params := new(struct {
		Namespace     string `json:"namespace" binding:"required"`
		ConfigMapData string `json:"configmap_data" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBindJSON(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		err = kubernetes.NewK8sInterface().CreteClusterConfigMap(cid, params.Namespace, params.ConfigMapData)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "创建成功")
		}
	}

}

/*列表*/
func GetClusterSecretList(ctx *gin.Context) {
	params := new(struct {
		Namespace string `form:"namespace" binding:"required"`
		// LabelSelector string `form:"label_selector"`
	})
	if err := ctx.ShouldBind(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
		return
	}
	cid := ctx.Param("cid")
	ComfigMapList, err := kubernetes.NewK8sInterface().GetClusterSecretList(cid, params.Namespace)
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
		return
	}
	NewSecretList := []map[string]interface{}{}
	for _, item := range ComfigMapList.Items {
		NewSecretList = append(NewSecretList, map[string]interface{}{
			"name":               item.Name,
			"namespace":          item.Namespace,
			"uid":                item.UID,
			"creation_timestamp": item.CreationTimestamp,
			"annotations":        item.Annotations,
		})

	}
	utils.ReturnContext(ctx).Successful("成功", NewSecretList)
}

/*详情*/
func GetClusterSecretInfo(ctx *gin.Context) {
	params := new(struct {
		Namespace  string `form:"namespace" binding:"required"`
		SecretName string `form:"secret_name" binding:"required"`
	})
	if err := ctx.ShouldBind(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
		return
	}
	cid := ctx.Param("cid")
	ComfigMap, err := kubernetes.NewK8sInterface().GetClusterSecretInfo(cid, params.Namespace, params.SecretName)
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		utils.ReturnContext(ctx).Successful("成功", ComfigMap)
	}

}

/*删除*/
func DeleteClusterSecret(ctx *gin.Context) {
	params := new(struct {
		Namespace  string `form:"namespace"  json:"namespace" binding:"required"`
		SecretName string `form:"secret_name" json:"secret_name" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
		return
	}
	err := kubernetes.NewK8sInterface().DeleteClusterSecret(cid, params.Namespace, params.SecretName)
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		utils.ReturnContext(ctx).Successful("成功", "删除成功")
	}
}

/*更新*/
func UpdateClusterSecret(ctx *gin.Context) {
	params := new(struct {
		Namespace  string `json:"namespace" binding:"required"`
		SecretData string `json:"secret_data" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
		return
	}
	err := kubernetes.NewK8sInterface().UpdateClusterSecret(cid, params.Namespace, params.SecretData)
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		utils.ReturnContext(ctx).Successful("成功", "更新成功")
	}
}

/*创建*/
func CreateClusterSecret(ctx *gin.Context) {
	params := new(struct {
		Namespace  string `json:"namespace" binding:"required"`
		SecretData string `json:"secret_data" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBindJSON(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		err = kubernetes.NewK8sInterface().CreteClusterSecret(cid, params.Namespace, params.SecretData)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "创建成功")
		}
	}
}
